Skip to content

Allow ASan container annotations to activate piecemeal#6211

Open
amyw-msft wants to merge 2 commits intomicrosoft:mainfrom
amyw-msft:partial-asan-annotation
Open

Allow ASan container annotations to activate piecemeal#6211
amyw-msft wants to merge 2 commits intomicrosoft:mainfrom
amyw-msft:partial-asan-annotation

Conversation

@amyw-msft
Copy link
Copy Markdown
Member

@amyw-msft amyw-msft commented Apr 3, 2026

Fixes #6186.

Previously, ASan annotations for string, vector, and optional were activated by linking with stl_asan.lib which set each activation variable to true, leading to ODR violations if some TUs were built with annotations inserted but partially activated.

This change removes the stl_asan.lib dependency and allows each option to set the activation variable individually.

I've retained stl_asan.lib and modified the definitions with __declspec(selectany) to match those in the headers, and also to ensure compatibility if linking with a static library from a previous version of the toolset.

The test I've included just ensures the annotations can be targeted individually instead of constructing an ASan error. I did this for simplicity (esp as new annotations are added) while still covering every scenario. I limited it to one disabled out of the set instead of exhaustive validation to not devote too much test time to this.

I've verified the scenario from #6186 locally, as well as ensuring it is backwards compatible with old versions of the headers (meaning, I built the annotated_lib.lib with an old toolset and test.obj with a toolset with this change and manually tried out every combination). Building a static library with the new toolset and linking with an old toolset produces a link error (the old stl_asan.lib does not contain __declspec(selectany), so clashes with the definition added in __msvc_sanitizer_annotate_container.hpp), but my understanding is that this isn't a supported scenario.

…ted piecemeal. The lib is retained for compatibility with static libraries built with old headers.
@amyw-msft amyw-msft added the bug Something isn't working label Apr 3, 2026
@amyw-msft amyw-msft requested a review from a team as a code owner April 3, 2026 22:15
@amyw-msft amyw-msft added the ASan Address Sanitizer label Apr 3, 2026
@github-project-automation github-project-automation bot moved this to Initial Review in STL Code Reviews Apr 3, 2026
@StephanTLavavej StephanTLavavej self-assigned this Apr 3, 2026
PM_CL="/EHsc /MTd /std:c++latest /permissive- /analyze:only /analyze:autolog-"
PM_CL="/EHsc /MTd /std:c++latest /permissive- /fp:strict"
PM_CL="/EHsc /MTd /std:c++latest /permissive-"
# TRANSITION, we don't use /ALTERNATENAME for Clang (see GH-5224) so we cannot test /D_ANNOTATE_VECTOR without -fsanitize=address
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment was copy-pasted from GH_002030_asan_annotate_vector/env.lst, but as you're crossing with PM_CL="/D_ANNOTATE_STL", I think this comment is inapplicable and should be removed here.

extern const bool _Asan_string_should_annotate = true;
extern const bool _Asan_vector_should_annotate = true;
extern const bool _Asan_optional_should_annotate = true;
// Retained for compatibility with old headers that add stl_asan.lib to the link.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use the magic words "TRANSITION, ABI".

Comment on lines +11 to +13
extern __declspec(selectany) const bool _Asan_string_should_annotate = true;
extern __declspec(selectany) const bool _Asan_vector_should_annotate = true;
extern __declspec(selectany) const bool _Asan_optional_should_annotate = true;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned that bundling these 3 definitions into a single object can still lead to scenarios where annotations are partially activated, the linker drags in asan.obj here, and (through the magic of selectany) we end up activating annotations that the user didn't request.

Conversely, I am even more concerned that asan_noop.cpp isn't being changed, and is still a monolithic source file producing a single object file.

Should we break up asan.cpp and asan_noop.cpp into 3 separate files each, prominently commented that they need to remain separate and never fused? stl_asan.lib would still be a single LIB for back-compat containing the 3 OBJs. The linker's behavior operates at an OBJ level of granularity.

// Use __declspec(selectany) to be compatible with new version that define these
// variables as __declspec(selectany) in __msvc_sanitizer_annotate_container.hpp.
// The new method is preferred because previously enabling just string would also
// enable vector and optional.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding ", see GH-6186." would be helpful (we try to avoid the need to git blame for programmer-archaeology).

extern const bool _Asan_vector_should_annotate = true;
extern const bool _Asan_optional_should_annotate = true;
// Retained for compatibility with old headers that add stl_asan.lib to the link.
// Use __declspec(selectany) to be compatible with new version that define these
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"with new version that define these" isn't grammatical. Should probably be "with the new version that defines these".


#ifdef _ACTIVATE_STRING_ANNOTATION
#pragma comment(lib, "stl_asan")
extern "C" __declspec(selectany) const bool _Asan_string_should_annotate = true;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of individually marking these as extern "C", I believe it would be better to move up the extern "C" { scope beginning at right line 148, so that these definitions look more similar to the declarations below. The only stuff in between are pragmas and undefs.

Comment on lines +2 to +3
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// REQUIRES: x64 || x86 || arm64
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: We conventionally put an empty line after the license banner.

@github-project-automation github-project-automation bot moved this from Initial Review to Work In Progress in STL Code Reviews Apr 10, 2026
@StephanTLavavej StephanTLavavej removed their assignment Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ASan Address Sanitizer bug Something isn't working

Projects

Status: Work In Progress

Development

Successfully merging this pull request may close these issues.

Partially disabling STL ASan annotations leads to harmful ODR violation

2 participants